import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import { Helper, Samples } from './src';

/**
 * 室内三维地图
 */
export class Map {
  /**
   * 初始化场景、相机、环境光源、平行光源、渲染器材
   * 持续渲染
   */
  constructor() {
    this.init();
    this.animate();
  }

  /**
   * 初始化
   */
  init() {
    // 场景
    this.scene = new THREE.Scene();
    // 相机
    this.camera = new THREE.PerspectiveCamera(
      75,
      window.innerWidth / window.innerHeight,
      1,
      50_0000
    );
    this.camera.position.set(0, 6_000, 6_000);
    this.camera.lookAt(0, 0, 0);
    // 场景添加相机
    this.scene.add(this.camera);
    // 环境光源
    const ambientLight = new THREE.AmbientLight(0xffffff, 1);
    this.scene.add(ambientLight);
    // 平行光源
    const directionalLight = new THREE.DirectionalLight(0xffffff, 1); //模拟远处类似太阳的光源
    directionalLight.position.set(0, 100, 0).normalize();
    this.scene.add(directionalLight);
    // 渲染器
    this.renderer = new THREE.WebGLRenderer({ antialias: true });
    this.renderer.shadowMap.enabled = true;
    this.renderer.setSize(window.innerWidth, window.innerHeight);
    this.renderer.setClearColor(0x4682b4, 1.0);
    document.body.appendChild(this.renderer.domElement);
    // 轨道控制器
    this.controls = new OrbitControls(this.camera, this.renderer.domElement);
    // this.controls.autoRotate = true;
    this.controls.update();
    // 坐标辅助
    const gridHelper = new Helper().grid();
    this.scene.add(gridHelper);
  }

  /**
   * 持续渲染
   * 动画帧
   */
  animate() {
    this.renderer.render(this.scene, this.camera);
    this.controls.update();
    requestAnimationFrame(this.animate.bind(this));
  }

  // 示例
  samples() {
    const samples = new Samples();
    // const single = samples.singleFloor();
    // const multi = samples.multiFloors();
    const wallWithWidnow = samples.wallWithWidnow();
    this.scene.add(wallWithWidnow);
  }
}
